var app = getApp()
var util = require('../../utils/utils.js');
const db = wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userID: '',
    _id: '',
    studentName: '',
    studentID: '',
    studentImg: '',
    reason: '',
    writeAllID: 0, //用来标识认证的状态，已通过 
    reason: '',
    examinedTime: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 点击放大图片
  previewImg: function (e) {
    let that = this;
    wx.previewImage({
      current: e.currentTarget.dataset.src, //当前图片地址
      //  urls:that.data.img   //所有要预览的图片的地址集合 数组形式
    })
  },
  myInputD(e) {
    var myData = e.detail.value
    this.setData({
      reason: myData
    })
  },
  //  不通过
  async failPass(e) {
    var that = this
    var v = that.data.reason
    const db = wx.cloud.database()
    if (v == "") {
      wx.showToast({
        title: '未添加理由',
        icon: "none"
      })
    } else {
      //确认发布
      let myRes = await db.collection('identify').doc(that.data._id).update({
        data: {
          reason: that.data.reason,
          examinedTime: util.formatTime(new Date()),
          writeAllID: 3
        },
      })
      let myRes2 = await db.collection('user').where({
        _openid:that.data._openid
      }).get()
      let myRes3 = await db.collection('user').doc(myRes2.data[0]._id).update({
        data: {
          writeAllID: 3, //审核不通过
        },
      })

      if (myRes3.errMsg == "document.update:ok") {
        let showModalRes = await wx.showModal({
          title: '成功',
          content: '您已经操作成功',
          showCancel: false
        })
        if (showModalRes.confirm) {
          console.log("点击了确认");
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    }
  },

  // 通过
  async successPass(event) {
    var that = this;
    var type = parseInt(event.currentTarget.dataset.type)
    if (type || that.data.reason) {
      let res = await db.collection('identify').doc(that.data._id).update({
        data: {
          reason: that.data.reason,
          examinedTime: util.formatTime(new Date()),
          writeAllID: 2
        },
      })
      let myRes2 = await db.collection('user').where({
        _openid:that.data._openid
      }).get()
      let myRes3 = await db.collection('user').doc(myRes2.data[0]._id).update({
        data: {
          writeAllID: 2, //审核通过
          isAdministrator: 2, //认证成功为会议室管理
          studentID: that.data.studentID
        },
      })
      if (myRes3.errMsg == "document.update:ok") {
        let showModalRes = await wx.showModal({
          title: '成功',
          content: '您已经操作成功',
          showCancel: false
        })
        if (showModalRes.confirm) {
          console.log("点击了确认");
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    }
  },
  onLoad: function (options) {
    var that = this
    that.setData({
      _id: options.id,
      _openid: options.openid
    })
    db.collection('identify').where({
      _id: options.id
    }).get().then(res => {
      that.setData({
        studentName: res.data[0].studentName,
        studentID: res.data[0].studentID,
        studentImg: res.data[0].studentImg,
        writeAllID: res.data[0].writeAllID,
        examinedTime: res.data[0].examinedTime,
        reason: res.data[0].reason
      })
    }).catch(err => {
      console.log(err)
    })
  },
})